$(function () {
    var carUrl = "/oa/t-car";
    var roleUrl = "/oa/t-role";
    var token = sessionStorage.getItem('token');
    var oprateFormatter = function (value, row, index) {
        return [
            '<button id="tb_edit" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">编辑</button>',
            '<button id="tb_delete" class="btn btn-default btn-xs" style="margin: 0 5px 0 5px">删除</button>',
        ].join("");
    };

    var oprateEvents = function(context) {
        var me = context;
        return {
            "click #tb_edit" : function(e, value, row, index) {
                me.edit([row]);
            },

            "click #tb_delete" : function(e, value, row, index) {
                me.remove([row]);
            },
        };
    };

    var stateFormatter = function (value, row, index) {
        if (row.carState == 0) {
            return "闲置";
        }else{
            return "锁定"
        }
    };

    var page = $.fn.cookePageTable({
        URL: carUrl,
        dialog: {
            domid: '#addModel',
            title: '车辆',
            validate : {
                carName: {
                    required: true,
                },
                // userEmail: {
                //     required: true,
                //     minlength: 4,
                //     maxlength: 128,
                //     email:true,
                // },
                // userPhone: {
                //     required: true,
                //     minlength: 3,
                //     maxlength: 19,
                //     digits:true,
                // },
            },
        },

        toolbar: {
            add: '#add',
            del: '#del',
            edit: '#edit',
            domid: '#toolbar',
            search: '#search',
        },

        table: {
            domid: "#table",
            uniqueId: "carId", //每一行的唯一标识，一般为主键列
            uniqdesc: function(row) {
                return row.carId;
            },                  //每一行的唯一标识描述字段信息
            columns: [{
                checkbox: true
            }, {
                field: 'carId',
                title: '车牌号码',
                filter: true,
            }, {
                field: 'carName',
                title: '车辆名称',
                filter: true,
            }, {
                field: 'carType',
                title: '车辆类型',
                filter: true,
            }, {
                field: 'carColor',
                title: '车辆颜色',
                filter: true,
            },  {
                field: 'createTime',
                title: '创建时间',
            }, {
                field: 'carState',
                title: '状态',
                formatter: stateFormatter,
            }, {
                field: 'operate',
                title: '操作',
                visible: true,
                events: oprateEvents,
                formatter: oprateFormatter,
            }]
        },
    });
});
